<template>
  <div style="padding: 10px">
<!--    功能区域-->
    <div style="margin: 10px 0;">
      <el-button type="primary" @click="add">新增</el-button>
      <el-button type="primary">导入</el-button>
      <el-button type="primary">导出</el-button>
    </div>
<!--    搜索区域-->
    <div style="margin: 10px 0">
      <el-input v-model="search" placeholder="输入关键字" style="width: 20%" clearable></el-input><!--clearable清空按钮-->
      <el-button type="primary" style="margin-left: 5px" @click="load">查询</el-button>
    </div>

      <el-table
          :data="tableData"
          border
          stripe
          style="width: 100%">
        <el-table-column
            prop="id"
            label="ID"
            sortable >
        </el-table-column>
        <el-table-column
            prop="name"
            label="房屋名称">
        </el-table-column>
        <el-table-column
            prop="price"
            label="房屋价格">
        </el-table-column>
        <el-table-column
            prop="room"
            label="房间数量">
        </el-table-column>
        <el-table-column
            prop="parlour"
            label="客厅数量">
        </el-table-column>
        <el-table-column
            prop="address"
            label="地址">
        </el-table-column>
        <el-table-column
            prop="status"
            label="房屋状态">
        </el-table-column>
        <!--操作按钮-->
        <el-table-column  label="操作"  >
          <template #default="scope">
            <el-button @click="handleEdit(scope.row)" link type="primary" size="small">编辑</el-button>
            <el-popconfirm title="确认删除吗?" @confirm="handleDelete(scope.row.id)">
              <template #reference>
                <el-button link size="small" style="color: darkred">删除</el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>

    <div style="margin: 10px 0">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[5, 10, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"/>

<!--      弹出新增对话框，通过变量dialogVisible来控制弹窗的开启或关闭-->
      <!--v-model用于绑定对象-->
      <el-dialog title="提示" v-model="dialogVisible" width="30%">
        <!--新增表单-->
        <el-form :model="form" label-width="120px">
          <!--表单项-->
          <el-form-item label="房屋名称">
            <el-input v-model="form.name" style="width: 80%"></el-input>
          </el-form-item>
          <el-form-item label="房屋价格">
            <el-input v-model="form.price" style="width: 80%"></el-input>
          </el-form-item>
          <el-form-item label="房间数量">
            <el-input v-model="form.room" style="width: 80%"></el-input>
          </el-form-item>
          <el-form-item label="客厅数量">
            <el-input v-model="form.parlour" style="width: 80%"></el-input>
          </el-form-item>

          <el-form-item label="地址">
            <el-input type="textarea" v-model="form.address" style="width: 80%"></el-input>
          </el-form-item>
          <el-form-item label="房屋状态">
            <el-radio v-model="form.status" label="未出租">未出租</el-radio>
            <el-radio v-model="form.status" label="已出租">已出租</el-radio>
            <!--            <el-input v-model="form.sex" style="width: 80%"></el-input>-->
          </el-form-item>
        </el-form>
        <template #footer>
          <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="save">确 定</el-button>
        </span>
        </template>
      </el-dialog>
    </div>
  </div>
</template>



<script>
//引入跨域请求 request.js 既是axios
import request from "../../utils/request";
// import request from "@/utils/request";

export default {
  name: 'HomeView',
  components: {

  },
  data() {
    return {
      form: {}, //绑定表单对象的form
      dialogVisible: false, //默认关闭新增弹窗
      search: '',
      currentPage: 1,
      pageSize: 10,
      total: 0,
      //这里的 tableData 对应着上面的 tableData
      tableData: []
    }
  },
  methods: {
    //加载时调用load方法，可以访问user的分页查询接口
    load() {
      request.get("/house",{
        params: { //get请求需要在这里加一个params参数
          pageNum: this.currentPage,
          pageSize: this.pageSize,
          search: this.search
        }
      }).then(res=>{  //将后台的数据赋值给res
        // console.log(res)
        this.tableData = res.data.records  //将res中数据中的records属性赋值给tableData即可
        this.total=res.data.total    //总条数
      })
    },

    add() {//新增方法，弹窗中新增，这里可以打开弹窗
      this.dialogVisible = true
      this.form = {} //清空表单
    },
    //新增或修改保存用户的方法
    save() {  //保存用户
      if(this.form.id){//如果所保存的对象有id，则为更新
        request.put("/house",this.form).then(res=>{
          if (res.code == '0') {  //如果后台返回的数据code为0表示更新成功，若不为0表示更新失败，需要将错误信息显示出来
            //提示信息
            this.$message({
              type: "success",
              message: "更新成功"
            })
          }else {
            this.$message({
              type: "error",
              message: res.msg
            })
          }
          // sessionStorage.setItem("house",JSON.stringify(res.data))
          this.load()//更新或者新增后刷新数据
          this.dialogVisible = false  //关闭弹窗
        })
      }else{//每页id为新增
        //将form作为请求参数返回
        request.post("/house",this.form).then(res=>{//这里的 .then 用于将前面的输出结果返回给res
          if (res.code == '0') {  //如果后台返回的数据code为0表示更新成功，若不为0表示更新失败，需要将错误信息显示出来
            //提示信息
            this.$message({
              type: "success",
              message: "新增成功"
            })
          }else {
            this.$message({
              type: "error",
              message: res.msg
            })
          }
        })
        this.load()//更新或者新增后刷新数据
        this.dialogVisible = false  //关闭弹窗
      }
    },
    //编辑方法
    handleEdit(row) {
      this.form = JSON.parse(JSON.stringify(row))//对form对象进行深拷贝，避免发生浅拷贝的问题
      this.dialogVisible = true;
    },
    //分页功能
    handleSizeChange(pageSize) {  //改变每页的个数触发
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum) {  //改变当前页码触发
      this.currentPage = pageNum
      this.load()
    },

    //删除方法
    handleDelete(id) {
      console.log(id)
      request.delete("/house/"+id).then(res=>{
        if (res.code == '0') {
          //提示信息
          this.$message({
            type: "success",
            message: "删除成功"
          })
        }else {
          this.$message({
            type: "error",
            message: res.msg
          })
        }
        this.load() //重新加载表格的数据
      })
    }
  },
  //页面加载时调用的方法created
  created() {
    this.load()
  }
}
</script>
